package com.mycoffee.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;




public class WSService
{
	 //瀹氫箟Handler瀵硅�?
    private static Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        //褰撴湁娑堟伅鍙戦�鍑烘潵鐨勬椂鍊欏氨鎵цHandler鐨勮繖涓柟娉�       
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//婢跺嫮鎮奤I
            switch (msg.what) {
                case 1:
                    String  s= (String) msg.obj;
                    Log.e("response", s);
//                    Toast.makeText(MainActivithis, s.toString(), Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    private String response;
    private static String rev;

    public static String WSResultSet(final String endpoint, final JSONObject object)
    {
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(endpoint);
                    //娣诲姞http澶翠俊鎭�?                   httppost.addHeader("Authorization", "your token"); //鐠併倛鐦塼oken
                    httppost.addHeader("Content-type", "application/json");
                    httppost.addHeader("User-Agent", "imgfornote");
                    httppost.setEntity(new StringEntity(object.toString(),"UTF-8"));
                    HttpResponse response;
                    response = httpclient.execute(httppost);

                    //妫�獙鐘舵�鐮侊紝濡傛灉鎴愬姛鎺ユ敹鏁版�?
                    int code = response.getStatusLine().getStatusCode();
                    System.out.println("code++++++++++"+code);
                    if (code == 200) {
                        rev = EntityUtils.toString(response.getEntity());//杩斿洖json鏍煎紡锛�{"id": "27JpL~j4vsL0LX00E00005","version": "abc"}
//                        System.out.println("rev"+rev);
//                obj = new JSONObject(rev);
//                String id = obj.getString("id");
//                String version = obj.getString("version");
                    }
                   else{
                        rev = "500";
                	
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Message message = new Message();
                message.what = 1;
                message.obj = rev.toString();

                handler.sendMessage(message);
            }
        }.start();
        return rev;

    }





}
